/*
 * @Author: worldzb 
 * @Date: 2018-11-26 23:53:16 
 * @Last Modified by: worldzb
 * @Last Modified time: 2018-11-29 17:43:19
 */

import Hash from '../../lib/hash'
import TableSettings from './TableSettings.class.js';

import Runner from '../../lib/runner';
import axios from 'axios';

class Table{
    
    constructor({
        active=true,
        id=new Hash().gethashcode(),
        title=`sheet${Math.random()}`,
        
        data=TableSettings.createData(10,10,false),
        
        mergeCells=[],
        formulas={},
        sourceURL=[],
        tempGetData=[],
        preHashTable=[],
        colWidths=[],
        rowHeights=[],

        settings=TableSettings.default(),
    }={}
    )
    {
        this.active=active; //是否为表格是否为激活状态
        this.id=id;  //独一无二的i
        
        this.title=title;//标题
        this.data=data;
        this.mergeCells=mergeCells;
        this.formulas=formulas;//公式支持
        this.sourceURL=sourceURL;
        this.tempGetData=tempGetData;
        this.preHashTable=preHashTable;
        this.settings=settings;
        
        this.colWidths=colWidths;
        this.rowHeights=rowHeights;

        this.variables={};
        this.loaded=()=>{};//加载完成后的钩子
        this.loading=(msg)=>{};

        this.initSetting();
    }

    initSetting(){
        this.settings.data=this.data;
        this.settings.mergeCells=this.mergeCells;
        this.settings.formulas.variables={};
        this.settings.colWidths=this.colWidths;
        this.settings.rowHeights=this.rowHeights;
        //this.settings.formulas=this.formulas;
    }

    //初始化表格的宽高
    initWidthAndHeight(){
        if(this.colWidths.length==0){
            for(let i in this.data.length){
                this.colWidths.push(100);
            }
        }
        if(this.rowHeights.length==0){
            for(let i in this.data[0].length){
                this.colWidths.push(50);
            }
        }
    }

    /**
     * 加载资源
     */
    loadSource(){
        if(this.sourceURL.length==0 || this.preHashTable==0) {
            this.loaded();
            this.loaded=()=>{};//运行完后进行销毁
            console.log(this.settings);
            return;
        };
        let that=this;
        //如果没有异步操作
        this.tempGetData=[];//先清零
        Runner.generator((function *(){
            if(that.sourceURL.length>0){
                for(let i in that.sourceURL){
                    let res=yield axios.get(that.sourceURL[i]);
                    
                    //将加载状态通过钩子函数传递除去
                    that.loading({
                        msg:`正在加载预定义变量 :${i}`
                    });

                    that.tempGetData.push(
                        res.data
                    )
                }
            }
            that.loadPreVal();
            //console.log( that.settings.formulas.variables,that.variables,that.settings);
        })());
    }

    /**
     * 加载预定义变量
     */
    loadPreVal(){
        let tempVal={};
        //console.log('PRE',this.preHashTable);
        //console.log('Temp',this.tempGetData);

        for(let i in this.preHashTable){
            let value=this.preHashTable[i].value;
            let t=this.tempGetData;
            for(let j in value){
                t=t[value[j]];
            }
            tempVal[this.preHashTable[i].key]=t;
            t=[];
            //console.log('tempVal',tempVal[this.preHashTable[i].key]);
        }
        
        this.settings.formulas.variables=tempVal;
        this.loading=()=>{};
        this.loaded();
        this.loaded=()=>{};//运行完后进行销毁
        
    }

    /**
     * 更新设置
     */
    updateSetting(){
        this.settings.data=this.data;
        this.settings.mergeCells=this.mergeCells;
    }

    /**
     * 设置单元格合并布局
     * @param {Array} data 合并列数据 为一个MergeRang数组
     */
    setMergeCells(data){
        this.mergeCells=data;
    }

    
    
}



export default Table;
